clearvars -except Globaloption option

if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

warning off

data = readtable('../../data/main_VAR/US_full.xlsx');
date = data.year;

startdatenum = 1793;
enddatenum = 2020;

startdate = find(date == startdatenum);
enddate = find(date == enddatenum);
T_post = length(date(startdate:enddate));

Tstart = startdate; 
Tend = enddate; 
T = Tend - Tstart + 1;

date = date(startdate:enddate);

data = data((data.year >= startdatenum - 1), :);
data = data((data.year <= enddatenum), :);

taxrevgdp = data.tau(2:end); % government tax revenue to GDP ratio
taxrevgdp0 = data.tau(1); % government tax revenue to GDP ratio
spendgdp = data.g(2:end); % government spending before interest exdp. to GDP ratio
spendgdp0 = data.g(1); % government spending before interest exp. to GDP ratio
surplusgdp = taxrevgdp - spendgdp; % primary surplus to gdp

figures.path = ['figures\US_'];

%% business cycle

peak = xlsread('../../data/supplement/US_bc_Davis2006.xlsx', 'sheet1', 'A4:A14');
trough = xlsread('../../data/supplement/US_bc_Davis2006.xlsx', 'sheet1', 'B4:B14');

ttime_date = [];
peak_date = [];
trough_date = [];

for i = 1793:2020
    ttime_date = [ttime_date; datenum(i, 12, 31)];
end

ttime_date = ttime_date(1:(end));

for i = 1:length(peak)
    peak_date = [peak_date, datenum(peak(i), 12, 31)];
    trough_date = [trough_date, datenum(trough(i), 12, 31)];
end

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on;
ylim([-20 40])

for tt = 1:length(peak_date)
    patch([ttime_date(find(ttime_date == peak_date(tt))) ttime_date(find(ttime_date == peak_date(tt))) ttime_date(find(ttime_date == trough_date(tt))) ttime_date(find(ttime_date == trough_date(tt)))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
    hold on
end

ha = plot(ttime_date, surplusgdp * 100, 'black', 'LineWidth', 1.5)
hold on
hb = plot(ttime_date, taxrevgdp * 100, 'red--', 'LineWidth', 1.5)
hold on
hc = plot(ttime_date, spendgdp * 100, 'b-.', 'LineWidth', 1.5)

hold on
xticks([datenum(1790:50:2020, 1, 1)]);
datetick('x', 'yyyy', 'keepticks');
xlim([datenum(1793, 1, 1) datenum(2020, 12, 31)])

ylabel('% of GDP')
tmp = ylim;
p = recessionplot;
ylim(tmp);

for i = 1:11
    set(get(get(p(i), 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
end

tmp = ylim;
p = recessionplot('recessions', [datenum(2020, 3, 1) datenum(2020, 4, 30); ]);
ylim(tmp);
set(get(get(p(1), 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
grid;
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');

if Globaloption.savefig == 1
    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/US/US_surpluses_recession', '-dpdf', '-fillpage');
end

%% wars

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on;
ylim([-30 90])
patch([date(find(date == 1812)) date(find(date == 1812)) date(find(date == 1815)) date(find(date == 1815))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1846)) date(find(date == 1846)) date(find(date == 1848)) date(find(date == 1848))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1861)) date(find(date == 1861)) date(find(date == 1865)) date(find(date == 1865))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1897)) date(find(date == 1897)) date(find(date == 1898)) date(find(date == 1898))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1939)) date(find(date == 1939)) date(find(date == 1945)) date(find(date == 1945))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
date2 = [date; 2021; 2022; 2023; 2024; 2025];
patch([date2(find(date2 == 2020)) date2(find(date2 == 2020)) date2(find(date2 == 2022)) date2(find(date2 == 2022))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

hold on
xlim([1790 2027])
ylabel('% of GDP')
ha = plot(date, surplusgdp * 100, 'black', 'LineWidth', 1.5);
hold on
hb = plot(date, taxrevgdp * 100, 'red--', 'LineWidth', 1.5)
hold on
hc = plot(date, spendgdp * 100, 'b-.', 'LineWidth', 1.5)
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');
gtext('War of 1812')
gtext('Mexican')
gtext('-American War')
gtext('Civil War')
gtext('Spanish')
gtext('-American War')
gtext('WWI')
gtext('WWII')
gtext('Covid')

grid on

if Globaloption.savefig == 1
    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/US/surplus_US', '-dpdf', '-fillpage');
end

%% Mktable debt
debtgdp = data.debt_gdp_sargenthall(2:end);

gdp_history = xlsread('../../data/supplement/USGDP_1790-2020.xlsx', 'USGDP_1790-2020', 'b6:b233', 'basic');

mktdebt = xlsread('../../data/supplement/US_Treasury_Debt_1776-2018.xlsx', 'debt_series_1776_2018', 'w1696:w2908', 'basic');
nonmktdebt = xlsread('../../data/supplement/US_Treasury_Debt_1776-2018.xlsx', 'debt_series_1776_2018', 'aa1696:aa2908', 'basic');

mktdebt = mktdebt(1:12:end) / 1000000; % december data
nonmktdebt = nonmktdebt(1:12:end) / 1000000;

totaldebtpost2018 = xlsread('../../data/supplement/govdebt.xlsx', 'data', 'E939:E951');
totaldebtpost2018 = totaldebtpost2018(1:12:end) * 1000;

debt_begindate = 1917;
debt_enddate = 2020;

mktdebtgdp = mktdebt ./ gdp_history(find(date == debt_begindate):find(date == 2018));
totaldebtgdp = [(mktdebt + nonmktdebt); totaldebtpost2018] ./ gdp_history(find(date == debt_begindate):find(date == debt_enddate));

% figure

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on;
ylim([-0.3 1.4])
patch([date(find(date == 1812)) date(find(date == 1812)) date(find(date == 1815)) date(find(date == 1815))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1846)) date(find(date == 1846)) date(find(date == 1848)) date(find(date == 1848))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1861)) date(find(date == 1861)) date(find(date == 1865)) date(find(date == 1865))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1897)) date(find(date == 1897)) date(find(date == 1898)) date(find(date == 1898))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1939)) date(find(date == 1939)) date(find(date == 1945)) date(find(date == 1945))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date2(find(date2 == 2020)) date2(find(date2 == 2020)) date2(find(date2 == 2022)) date2(find(date2 == 2022))], [min(ylim + 0.01) max(ylim - 0.01) max(ylim - 0.01) min(ylim + 0.01)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

hold on
ha = plot(date, debtgdp, 'black', 'LineWidth', 1.5)
hold on
hb = plot([1917:1:2020], totaldebtgdp, 'black--', 'LineWidth', 1.5)
grid on
xlim([1790 2027])
hleglines = [ha hb];
hleg = legend(hleglines, 'Marketable Debt', 'Marketable + Nonmarketable', 'Location', 'southeast');
gtext('War of 1812')
gtext('Mexican')
gtext('-American War')
gtext('Civil War')
gtext('Spanish')
gtext('-American War')
gtext('WWI')
gtext('WWII')

gtext('Covid')
grid on

if Globaloption.savefig == 1
    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/US/US_debtgdp', '-dpdf', '-fillpage')
end
